package com.digital.herbarium.Controller;

import com.digital.herbarium.Entity.*;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.annotation.Log;
import com.digital.herbarium.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/general")
public class GeneralController {

    @Autowired
    private SpecimenService specimenService;
    @Autowired
    private ImageService imageService;
    @Autowired
    private PopularService popularService;
    @Autowired
    private AtlasService atlasService;
    @Autowired
    private DnaService dnaService;

    @GetMapping("generalSerach")
    @Log("general")
    public ResponseResult generalSerach(Specimen specimen,
                                        @RequestParam(value = "type",defaultValue = "0",required = false)int type,
                                        @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                        @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
       // boolean b = CheckObjectIsNullUtils.objCheckIsNull(specimen);
        if(specimen.getSearchKey() == null || specimen.getSearchKey().isEmpty()){
            throw new CustomException(400,"请输入查询条件");
        }
        HashMap<String,Object> map = new HashMap<>();
        if(type == 0){
            PageInfo<Specimen> specimenInfo = specimenService.getSpecimenInfo(specimen, pageSize, pageNum);
            //原植物图库
//            List<Object> list1 = new ArrayList<>();
            Image image = new Image();
            image.setCnName(specimen.getSearchKey());
            PageInfo<Image> imageInfo = imageService.getImageInfo(image, pageSize, pageNum);
            //图谱库
            Atlas atlas = new Atlas();
            atlas.setSearchKey(specimen.getSearchKey());
            PageInfo<Atlas> alist = atlasService.getAtlasInfo(atlas, pageSize, pageNum);
            //DNA序列库
            Dna dna = new Dna();
            dna.setSearchKey(specimen.getSearchKey());
            PageInfo<Dna> dlist = dnaService.getDnaInfo(dna, pageSize, pageNum);
            //科普宣传
            Popular popular = new Popular();
            popular.setTitle(specimen.getSearchKey());
            PageInfo<Popular> plist = popularService.getPopularInfo(popular,pageSize,pageNum);
            map.put("type",type);
            map.put("specimenInfo",specimenInfo);
            map.put("imageInfo",imageInfo);
            map.put("atlasLibrary",alist);
            map.put("dna",dlist);
            map.put("popularScience",plist);
        }else if(type == 1){
            PageInfo<Specimen> specimenInfo = specimenService.getSpecimenInfo(specimen, pageSize, pageNum);
            map.put("type",type);
            map.put("specimenInfo",specimenInfo);
        }else if(type == 2){
            Image image = new Image();
            image.setCnName(specimen.getSearchKey());
            PageInfo<Image> imageInfo = imageService.getImageInfo(image, pageSize, pageNum);
            map.put("type",type);
            map.put("imageInfo",imageInfo);
        }else if (type == 3){
            //科普宣传
            Popular popular = new Popular();
            popular.setTitle(specimen.getSearchKey());
            PageInfo<Popular> plist = popularService.getPopularInfo(popular,pageSize,pageNum);
            map.put("type",type);
            map.put("popularScience",plist);
        }else  if(type == 4 ){
            //图谱库
            Atlas atlas = new Atlas();
            atlas.setSearchKey(specimen.getSearchKey());
            PageInfo<Atlas> alist = atlasService.getAtlasInfo(atlas, pageSize, pageNum);
            map.put("type",type);
            map.put("atlasLibrary",alist);
        }else if(type == 5){
            //DNA序列库
            Dna dna = new Dna();
            dna.setSearchKey(specimen.getSearchKey());
            PageInfo<Dna> dlist = dnaService.getDnaInfo(dna, pageSize, pageNum);
            map.put("type",type);
            map.put("dna",dlist);
        }




        return ResponseResult.SUCCESS().setData(map);
    }
}
